package sisi.enquires;
/**
 * NotesuppseqtController.java - Controller class for the <i>Notesuppseqt database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 24/03/2014
 * 
 */
import java.util.List;

import javax.persistence.EntityManager;

public class NotesuppseqtController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Notesuppseqt[] get(String cerca) {
		List<Notesuppseqt> lista = getList(cerca);
			return lista.toArray(new Notesuppseqt[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Notesuppseqt> getList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.note) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Notesuppseqt as c " + cWhere 
					+ " order by c.id ");

			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}

			List<Notesuppseqt> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	
	public Notesuppseqt getxIdReqEIdSupplier(int idReq, int idSupplier, int seq) {
		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Notesuppseqt as c WHERE c.idrequest = " + idReq + " AND c.idsupplier = " + idSupplier + " AND c.seq = "+seq );

			Notesuppseqt note = new Notesuppseqt();
			if (q.getResultList().size()==1){
				note = (Notesuppseqt)q.getSingleResult();
			}
			return note;
		} finally {
			em.close();
		}
	}
	
	public Notesuppseqt getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Notesuppseqt notesuppseqtx = em.find(Notesuppseqt.class, id);
			return notesuppseqtx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Notesuppseqt add(Notesuppseqt notesuppseqt) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(notesuppseqt);
			em.getTransaction().commit();
			notesuppseqt = refresh(notesuppseqt);
		} finally {
			em.close();
			return notesuppseqt;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Notesuppseqt notesuppseqt) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Notesuppseqt notesuppseqtx = em.find(Notesuppseqt.class, notesuppseqt.getId());
			notesuppseqtx.setId(notesuppseqt.getId());
			notesuppseqtx.setSeq(notesuppseqt.getSeq());
			notesuppseqtx.setNote(notesuppseqt.getNote());
			notesuppseqtx.setIdrequest(notesuppseqt.getIdrequest());
			notesuppseqtx.setIdsupplier(notesuppseqt.getIdsupplier());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Notesuppseqt refresh(Notesuppseqt notesuppseqt) {

		Notesuppseqt notesuppseqtx = notesuppseqt;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			notesuppseqtx = em.find(Notesuppseqt.class, notesuppseqt.getId());
			em.flush();
			em.refresh(notesuppseqtx);
		} finally {
			em.close();
			return notesuppseqtx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Notesuppseqt notesuppseqt) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Notesuppseqt notesuppseqtx = em.find(Notesuppseqt.class, notesuppseqt.getId());
			em.remove(notesuppseqtx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
